const fetch = require('../../utils/fetch.js')

Page({
  data: {
    category:{},
    shops:[],
    pageindex:0,
    pagesize:20,
    hasmore:true,
    id :1
  },

  loadmore() {
    if(!this.data.hasmore) return

    let {pageindex,pagesize} = this.data
    const parmas = {_page: ++pageindex,_limit: pagesize}
    return fetch(`categories/${this.data.category.id}/shops`,parmas).then(res => {
      const totalcount = parseInt(res.header['X-Total-Count'])
      const hasmore = pageindex * pagesize < totalcount
      const shops = this.data.shops.concat(res.data)
      this.setData({shops,hasmore,pageindex})
    })
  },

  onLoad: function (options) {
    this.setData({id:options.cat})
  },
  
  onReady: function () {
    // console.log( this.data.id, 88888)
    fetch(`categories/${this.data.id}`).then(res => {
      this.setData({category:res.data})
      wx.setNavigationBarTitle({
        title: res.data.name
      })
      this.loadmore()
    })
  },
  onReachBottom : function(){
    this.loadmore()
  },
  onPullDownRefresh : function(){
    this.setData({shops:[],pageindex:0,hasmore:true})
    this.loadmore().then(() => wx.stopPullDownRefresh())
  }


})